Skip to content

Conversation

bjorn3
Copy link
Member

@bjorn3 bjorn3 commented Oct 9, 2025

In the future this should make it easier to use weak symbols for the allocator shim on platforms that properly support weak symbols. And it would allow reusing the allocator shim code for handling default implementations of the upcoming externally implementable items feature on platforms that don't properly support weak symbols.

In addition to make this possible, the alloc error handler is now handled in a way such that it is possible to avoid using the allocator shim when liballoc is compiled without no_global_oom_handling if you use #[alloc_error_handler]. Previously this was only possible if you avoided liballoc entirely or compiled it with no_global_oom_handling. You still need to avoid libstd and to define the symbol that indicates that avoiding the allocator shim is unstable.

@rustbot
Copy link
Collaborator

rustbot commented Oct 9, 2025

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

Some changes occurred in tests/ui/sanitizer

cc @rcvalle

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-run-make Area: port run-make Makefiles to rmake.rs PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 9, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 9, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@bjorn3
Copy link
Member Author

bjorn3 commented Oct 9, 2025

cc @anforowicz if this gets merged, https://source.chromium.org/chromium/chromium/src/+/main:build/rust/allocator/lib.rs;l=99-107 would need to be removed to avoid a symbol conflict as the definition gets moved from the allocator shim to libstd.

cc @jdonszelmann as I believe this will help a bit with externally implementable items.

@rust-log-analyzer

This comment has been minimized.

@bjorn3
Copy link
Member Author

bjorn3 commented Oct 9, 2025

I pushed a fix for the miri failures, but it only shows up at https://github.com/bjorn3/rust/tree/alloc_shim_weak_shape, not on this PR.

@bjorn3 bjorn3 force-pushed the alloc_shim_weak_shape branch from 98c96d6 to 614496e Compare October 9, 2025 15:29
@rustbot
Copy link
Collaborator

rustbot commented Oct 9, 2025

The Miri subtree was changed

cc @rust-lang/miri

@bjorn3 bjorn3 force-pushed the alloc_shim_weak_shape branch from 2684fab to 98c96d6 Compare October 9, 2025 15:30
!any_dynamic_crate
}

pub fn allocator_shim_contents(tcx: TyCtxt<'_>, kind: AllocatorKind) -> Vec<AllocatorMethod> {
Copy link
Member

@RalfJung RalfJung Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of replicating this logic in Miri, could we have Miri call this function to figure out what to forward where?

I'm imagining something like the following field in the machine:

/// Indicates which functions should be forwarded to which other functions
/// to emulate what would usually be the allocator shim module during codgen.
allocator_shims: FxHashSet<String, String>,

This can then be filled once on startup (so we can use global_fn_name/default_fn_name and it doesn't matter much that they allocate), and when a shim is called we just do a quick lookup.

That is quite different from how we currently handle the ALLOCATOR_METHODS... there we want custom shims for the defaults, not a symbol lookup. This matches the let alloc_attr_flag = match method.name { in codegen_llvm. Given that this is semantically relevant, would be nice to also have the logic shared that marks some of these shims as "magic", and then find a way to use that from Miri.

I guess that's all better done in a future PR, but it seems better than having to duplicate this logic in Miri.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would be a good idea. And it would become actually necessary to do it that way if externally implementable items were to start using this code as in that case there wouldn't be a fixed set of functions we can hard code in miri.

@bjorn3 bjorn3 force-pushed the alloc_shim_weak_shape branch from 614496e to 238f3d2 Compare October 10, 2025 12:38
@bjorn3
Copy link
Member Author

bjorn3 commented Oct 10, 2025

Dropped the commit for reworking how -Zoom works and added the suggested comments.

bjorn3 and others added 3 commits October 10, 2025 13:04
Currently it is possible to avoid linking the allocator shim when
__rust_no_alloc_shim_is_unstable_v2 is defined when linking rlibs
directly as some build systems need. However this requires liballoc to
be compiled with --cfg no_global_oom_handling, which places huge
restrictions on what functions you can call and makes it impossible to
use libstd. Or alternatively you have to define
__rust_alloc_error_handler and (when using libstd)
__rust_alloc_error_handler_should_panic
using #[rustc_std_internal_symbol]. With this commit you can either use
libstd and define __rust_alloc_error_handler_should_panic or not use
libstd and use #[alloc_error_handler] instead. Both options are still
unstable though.

Eventually the alloc_error_handler may either be removed entirely
(though the PR for that has been stale for years now) or we may start
using weak symbols for it instead. For the latter case this commit is a
prerequisite anyway.
In the future this should make it easier to use weak symbols for the
allocator shim on platforms that properly support weak symbols. And it
would allow reusing the allocator shim code for handling default
implementations of the upcoming externally implementable items feature
on platforms that don't properly support weak symbols.
Co-Authored-By: Ralf Jung <post@ralfj.de>
@bjorn3 bjorn3 force-pushed the alloc_shim_weak_shape branch from 238f3d2 to 2e25b58 Compare October 10, 2025 13:05
sym::dealloc => CodegenFnAttrFlags::DEALLOCATOR,
sym::realloc => CodegenFnAttrFlags::REALLOCATOR,
sym::alloc_zeroed => CodegenFnAttrFlags::ALLOCATOR_ZEROED,
_ => CodegenFnAttrFlags::empty(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_ => CodegenFnAttrFlags::empty(),
ALLOC_ERROR_HANDLER => CodegenFnAttrFlags::empty(),

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alloc, dealloc, realloc and alloc_zeroed are special cases. In the future the allocator shim may have an unbounded set of functions forwarding to default implementations if externally implementable items were to use it.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 10, 2025
Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Miri changes LGTM.

View changes since this review

@bjorn3
Copy link
Member Author

bjorn3 commented Oct 10, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-run-make Area: port run-make Makefiles to rmake.rs PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants